package com.ceo.common.utils;

import cn.hutool.core.util.StrUtil;
import com.ceo.common.http.exception.PlatformException;
import com.ceo.common.threadlocal.EnvironEntity;
import com.ceo.common.threadlocal.ThreadEnvUtil;
import com.ceo.common.vo.UserVo;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/24 10:53
 * Copyright (c)  All rights reserved
 */
public class EnvUtil {
    private static final Logger logger = LoggerFactory.getLogger(EnvUtil.class);


    public static String getCurrentToken() {
        return EnvironEntity.getToken();
    }

    public static String getCurClientType() {
        return EnvironEntity.getClientType();
    }

    /*
       获取当前用户语言
       从请求头的 language 取语言属性
       返回 default、 en、zh等
     */
    public static String getCurLanguage() {
        String language = EnvironEntity.getLanguage();
        if (StrUtil.isEmpty(language)) {
            language = "zh_cn";
        }
        return language;
    }

    /*
       这里 返回当前登录用户的id，如果需要获取当前登录用户的其它信息，从 getCurUserObject 中获取
    */
    public static String getCurUser() {
        return getCurUserObject().getId();
    }

    public static UserVo getCurUserObject() {
        UserVo userVo = ThreadEnvUtil.getUserObject();
        if (userVo != null && !Strings.isNullOrEmpty(userVo.getId())) {
            return userVo;
        }
        throw new PlatformException("用户未登录", 11010000);
    }



}
